package com.jackykeke.opengldemo.ndkutils;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.SurfaceView;

import com.jackykeke.opengldemo.renderer.MyGLRender;
import com.jackykeke.opengldemo.renderer.MyNativeRender;

import static com.jackykeke.opengldemo.renderer.MyNativeRender.*;


public class MyGLSurfaceView extends GLSurfaceView implements ScaleGestureDetector.OnScaleGestureListener {
    private static final String TAG = "MyGLSurfaceView";
    private final ScaleGestureDetector mScaleGestureDetector;
    private MyGLRender mGLRender;

    public static final int IMAGE_FORMAT_RGBA = 0x01;
    public static final int IMAGE_FORMAT_NV21 = 0x02;
    public static final int IMAGE_FORMAT_NV12 = 0x03;
    public static final int IMAGE_FORMAT_I420 = 0x04;

    private float mPreScale = 1.0f;
    private float mCurScale = 1.0f;
    private long mLastMultiTouchTime = 0;

    private float mPreviousY = 0;
    private float mPreviousX = 0;

    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;

    private int mXAngle;
    private int mYAngle;

    private int mRatioWidth = 0;
    private int mRatioHeight = 0;


    public MyGLSurfaceView(Context context, MyGLRender myGLRender) {
        this(context, myGLRender, null);
    }


    public MyGLSurfaceView(Context context, MyGLRender myGLRender, AttributeSet attrs) {
        super(context, attrs);

        setEGLContextClientVersion(3);

        mGLRender = myGLRender;

        setEGLConfigChooser(8, 8, 8, 8, 16, 8);
        setRenderer(mGLRender);
        setRenderMode(RENDERMODE_WHEN_DIRTY);

        mScaleGestureDetector = new ScaleGestureDetector(context, this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent");

        if (event.getPointerCount() == 1) {
            consumeTouchEvent(event);

            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis - mLastMultiTouchTime > 2000) {
                float x = event.getX();
                float y = event.getY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        float dy = y - mPreviousY;
                        float dx = x - mPreviousX;
                        mYAngle += dx * TOUCH_SCALE_FACTOR;
                        mXAngle += dy * TOUCH_SCALE_FACTOR;
                }

                mPreviousX = x;
                mPreviousY = y;

//                switch (mGLRender.getmSampleType()) {
//                    case SAMPLE_TYPE_FBO_LEG:
//                    case SAMPLE_TYPE_COORD_SYSTEM:
//                    case SAMPLE_TYPE_BASIC_LIGHTING:
//                    case SAMPLE_TYPE_TRANS_FEEDBACK:
//                    case SAMPLE_TYPE_MULTI_LIGHTS:
//                    case SAMPLE_TYPE_DEPTH_TESTING:
//                    case SAMPLE_TYPE_INSTANCING:
//                    case SAMPLE_TYPE_STENCIL_TESTING:
//                    case SAMPLE_TYPE_PARTICLES:
//                    case SAMPLE_TYPE_SKYBOX:
//                    case SAMPLE_TYPE_3D_MODEL:
//                    case SAMPLE_TYPE_PBO:
//                    case SAMPLE_TYPE_KEY_VISUALIZE_AUDIO:
//                        mGLRender.updateTransformMatrix(mXAngle, mYAngle, mCurScale, mCurScale);
//                        requestRender();
//                        break;
//                    default:
//                        break;
//
//                }
            }

        } else {
            mScaleGestureDetector.onTouchEvent(event);
        }
        return true;

    }

    private void consumeTouchEvent(MotionEvent event) {

        dealClickEvent(event);

//        float touchX = -1, touchY = -1;
    }

    private void dealClickEvent(MotionEvent event) {

        float touchX = -1, touchY = -1;

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                touchX = event.getX();
                touchY = event.getY();

//                switch (mGLRender.getmSampleType()) {
//                    case MyNativeRender.SAMPLE_TYPE_KEY_SCRATCH_CARD:
//                        mGLRender.setTouchLoc(touchX, touchY);
//                        requestRender();
//                        break;
//                    default:
//                        break;
//                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        Log.d(TAG, "onScale");

//        switch (mGLRender.getmSampleType()) {
//            case SAMPLE_TYPE_COORD_SYSTEM:
//            case SAMPLE_TYPE_BASIC_LIGHTING:
//            case SAMPLE_TYPE_INSTANCING:
//            case SAMPLE_TYPE_3D_MODEL:
//            case SAMPLE_TYPE_KEY_VISUALIZE_AUDIO: {
//                float preSpan = detector.getPreviousSpan(); //跨度
//                float curSpan = detector.getCurrentSpan();
//                if (curSpan < preSpan) {
//                    mCurScale = mPreScale - (preSpan - curSpan) / 200;
//                } else {
//                    mCurScale = mPreScale + (curSpan - preSpan) / 200;
//                }
//
//                mCurScale = Math.max(0.05f, Math.min(mCurScale, 80.0f));
//                mGLRender.updateTransformMatrix(mXAngle, mYAngle, mCurScale, mCurScale);
//                requestRender();
//            }
//            break;
//            default:
//                break;
//        }
        return false;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        Log.d(TAG, "onScaleBegin");

        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        Log.d(TAG, "onScaleEnd");

        mPreScale = mCurScale;
        mLastMultiTouchTime = System.currentTimeMillis();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d(TAG, "onMeasure");

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }

        }
    }

    public void setAspectRatio(int width, int height) {
        Log.d(TAG, "setAspectRatio() called with: width = [" + width + "], height = [" + height + "]");
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }

        mRatioWidth = width;
        mRatioHeight = height;
        requestLayout();
    }


}
